import db from '../../../datastore'
import lang from '../../i18n'

const state = {
  skin: 'skin-dark', // 主题风格
  lang: 'zh', // 语言设置
  showalert: false, // 显示全局alert信息
  altitle: '提示', // 全局alert的标题信息
  almsg: '', // 全局alert提示信息
  altype: 1, // 全局alert提示类型 1常规提示 2警告提示 3错误提示
  isloading: false, // 全局显示loading
  loadingmsg: '拼命加载中', // 全局loading提示信息
  rightClick: false, // 是否显示鼠标右键菜单
  tabIndex: 1, // 行情列表切换: 全部/自选
  optional: [], // 用户自选合约列表
  isWinMax: true, // 是否是窗口最大化
}

const mutations = {
  // 设置窗口最大化
  SET_ISWINMAX: (state, ismax) => {
    state.isWinMax = ismax
  },
  // 设置主题
  SET_SKIN: (state, skin) => {
    state.skin = skin
  },
  // 设置语言
  SET_LANG: (state, lang) => {
    state.lang = lang
  },
  SET_ALERT: (state, alert) => {
    state.showalert = alert
  },
  SET_ALTITLE: (state, title) => {
    state.altitle = title
  },
  SET_ALMSG: (state, msg) => {
    state.almsg = msg
  },
  SET_ALTYPE: (state, type) => {
    state.altype = type
  },
  SET_ISLOADING: (state, stus) => {
    state.isloading = stus
  },
  SET_LOADINGMSG: (state, msg) => {
    state.loadingmsg = msg
  },
  SET_RIGHTMENU: (state, isShow) => {
    state.rightClick = isShow
  },
  SET_TABINDEX: (state, index) => {
    state.tabIndex = index
  },
  SET_OPTIONAL: (state, list) => {
    state.optional = list
  }
}

const actions = {
  switchSkin: ({ commit }, nowskin) => {
    commit('SET_SKIN', nowskin)
  },
  switchLang: ({ commit }, nowlang) => {
    commit('SET_LANG', nowlang)
  },
  switchAlert: ({ commit, state }, visible) => {
    let { title, msg, type } = visible
    if (title) {
      commit('SET_ALTITLE', title)
    } else {
      commit('SET_ALTITLE', lang[state.lang].tip.tip)
    }
    // 修改必填功能
    commit('SET_ALERT', true)
    commit('SET_ALMSG', msg)
    commit('SET_ALTYPE', type)
  },
  closeAlert: ({ commit }) => {
    commit('SET_ALERT', false)
  },
  changeLoadings: ({ commit }, obj) => {
    // 修改loading显示状态，以及提示信息
    if (obj.msg) {
      commit('SET_LOADINGMSG', obj.msg)
    }
    commit('SET_ISLOADING', obj.status)
  },
  updateOptional: ({ commit, state }, obj) => {
    let [id, type] = obj;
    let newData = []

    // 新增
    if (type === 1) {
      for (const item of state.optional) {
        newData.push(item)
      }
      newData.push(id)
    }

    // 删除
    if (type === 2) {
      for (const val of state.optional) {
        if (val !== id) {
          newData.push(val)
        }
      }
    }

    // 去重
    let setArr = new Set(newData);
    let noRepeat = Array.from(setArr); // 无重复数组

    if (type === -1) {
      // 当前为初始化
      commit('SET_OPTIONAL', id)
    } else {
      commit('SET_OPTIONAL', noRepeat)
      // 写入本地数据库
      db
        .read()
        .set("users.optional", noRepeat)
        .write();
    }
  },
  updateWinMax: ({ commit }, max) => {
    commit('SET_ISWINMAX', max)
    db
      .read()
      .set("users.zoom", max)
      .write(); // 写入最小化配置
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
